﻿/*
 * 
 * 编写人：         江彦
 * 编写时间：       2007-04-03 10：00
 * 编写理由：           由于目前.Net自带的GridView控件的分页功能是通过将整个数据集载入然后跳转到指定的数据页来实现。因此，有
 *                  数据量大，速度慢等特点。现在，通过SQL Server 2005自带的Row_Number()函数来实现获取指定数据页的数据，降低
 *                  载入的数据量以及优化效率。
 * 
 * 备注：           在空数据的情况下，使用Row_Number()会出错，需要引起注意。
 * 
 */
using System;
using System.Data;
using System.ComponentModel;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using Jumpton.Persistent;

/// <summary>
/// 分页控件，和一个GridView关联起来可以实现分页
/// </summary>
[ToolboxItemFilter("System.Web.UI", ToolboxItemFilterType.Require)]
public partial class Controls_GridPager : System.Web.UI.UserControl
{
    private GridView grid;
    protected void Page_Load(object sender, EventArgs e)
    {
        if(ViewState[UniqueID + "_GridView"] != null)
        {
            grid = (GridView)NamingContainer.FindControl(ViewState[UniqueID + "_GridView"].ToString());
            
        }
        SetState();
    }


    /// <summary>
    /// 数据源
    /// DataTable,DataView ,DataSet ,DataViewManager ,任何实现 IListSource 接口的组件 ,任何实现 IList 接口的组件 
    /// </summary>
    public object DataSource
    {
        get
        {
            return ViewState[UniqueID + "_DataSource"];
        }
        set
        {
            ViewState[UniqueID + "_DataSource"] = value;
        }
    }




   

    public void BindData()
    {
        int count = 0;
        bool IsAddEmplyRow = false;
        if (DataSource != null)
        {
            if (DataSource is IList)
                count = (DataSource as IList).Count;
            else if (DataSource is IListSource)
                count = ((IListSource)DataSource).GetList().Count;
        }
        
        
        ViewState[this.UniqueID + "_Count"] = count;
        int pagecount = 0;
        if (count > 0)
            pagecount = count / PageSize;
        if(pagecount* PageSize< count)
            pagecount += 1;
        PageCount = pagecount;
        
        GridView.DataSource = DataSource;

        if (CurrentPage > pagecount)
        {
            CurrentPage = pagecount;
        }
        else if (CurrentPage < 0)
        {
            CurrentPage = 0;
        }

        grid.PageIndex = CurrentPage;
        grid.DataBind();
        if (IsAddEmplyRow)
        {
            grid.Rows[0].Cells.Clear();
        }
        SetState();
        txtInfo.Text = string.Format("总共<font color=blue>{0}</font>条记录&nbsp;&nbsp;共{1}页&nbsp;&nbsp;当前第{2}页", count, pagecount, CurrentPage + 1);
    }

    /// <summary>
    /// 设置导航按钮状态
    /// </summary>
    private void SetState()
    {
        if (PageCount == 0 )
        {
            btnFirst.Enabled = false;
            btnLast.Enabled = false;
            btnPrevious.Enabled = false;
            btnNext.Enabled = false;
            btnGoto.Enabled = false;
            txtPage.ReadOnly = true;

        }
        else if (PageCount == 1)
        {
            btnFirst.Enabled = false;
            btnLast.Enabled = false;
            btnPrevious.Enabled = false;
            btnNext.Enabled = false;
            btnGoto.Enabled = true;
            txtPage.ReadOnly = false;
        }
        else
        {
            btnGoto.Enabled = true;
            txtPage.ReadOnly = false;
            if (CurrentPage == 0)
            {
                btnFirst.Enabled = false;
                btnLast.Enabled = true;
                btnPrevious.Enabled = false;
                btnNext.Enabled = true;

            }
            else if (CurrentPage == PageCount-1)
            {
                btnFirst.Enabled = true;
                btnLast.Enabled = false;
                btnPrevious.Enabled = true;
                btnNext.Enabled = false;
            }
            else
            {
                btnFirst.Enabled = true;
                btnLast.Enabled = true;
                btnPrevious.Enabled = true;
                btnNext.Enabled = true;
            }
        }
    }


    /// <summary>
    /// 获取或者设置需要绑定的GridView控件
    /// </summary>
    [Browsable(true),Bindable(false) ]
    public GridView GridView
    {
        get
        {
            return grid;
        }
        set
        {
            grid = value;
            if (grid != null)
            {
                ViewState[UniqueID + "_GridView"] = grid.ID;
                grid.GridLines = GridLines.None;
                PageSize = grid.PageSize;
            }
        }
    }

   

    /// <summary>
    /// 获取数据集的数据页数
    /// </summary>
    public int PageCount
    {
        get
        {
            int count=0;
            if (ViewState[this.UniqueID + "_PageCount"] != null)
            {
                int.TryParse(ViewState[this.UniqueID + "_PageCount"].ToString(), out count);
            }
            return count;
        }
        set
        {
            ViewState[this.UniqueID + "_PageCount"] = value;
        }
        
    }

    /// <summary>
    /// 获取数据集的数据条数
    /// </summary>
    public int Count
    {
        get
        {
            int count = 0;
            if (ViewState[this.UniqueID + "_Count"] != null)
            {
                int.TryParse(ViewState[this.UniqueID + "_Count"].ToString(), out count);
            }
            return count;
        }
       
    }

      /// <summary>
    /// 获取和设置当前的数据页
    /// </summary>
    public int CurrentPage
    {
        get
        {
            int currentpage = 0;
            if (ViewState[this.UniqueID + "_CurrentPage"] != null)
                int.TryParse(ViewState[this.UniqueID + "_CurrentPage"].ToString(), out currentpage);
            else
                ViewState[this.UniqueID + "_CurrentPage"] = 0;
            return currentpage;
        }
        set
        {
            ViewState[this.UniqueID + "_CurrentPage"] = value;
        }
    }

    /// <summary>
    /// 获取或设置每页显示的数据量
    /// </summary>
    [Browsable(true),Category("Page")]
    public int PageSize
    {
        get
        {
            int pageSize = 10;
            if (ViewState[this.UniqueID + "_PageSize"] != null)
                int.TryParse(ViewState[this.UniqueID + "_PageSize"].ToString(), out pageSize);
            return pageSize;
        }
        set
        {
            ViewState[this.UniqueID + "_PageSize"] = value;
        }
    }

    /// <summary>
    /// 跳转到第一页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnFirst_Click(object sender, EventArgs e)
    {
        CurrentPage = 0;
        BindData();
        
    }

    /// <summary>
    /// 跳转到上一页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnPrevious_Click(object sender, EventArgs e)
    {
        if (CurrentPage != 0)
            CurrentPage -= 1;
        BindData();
    }
    /// <summary>
    /// 跳转到下一页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnNext_Click(object sender, EventArgs e)
    {
        if (CurrentPage != PageCount-1)
            CurrentPage += 1;
        BindData();
    }
    /// <summary>
    /// 跳转到最后一页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnLast_Click(object sender, EventArgs e)
    {
        CurrentPage = PageCount - 1;
        BindData();
    }

    /// <summary>
    /// 跳转到指定页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnGoto_Click(object sender, EventArgs e)
    {
        int page = CurrentPage;
        int.TryParse(txtPage.Text, out page);
        if (page >= 1 &&  page <= PageCount)
        {
            CurrentPage = page - 1;
            BindData();
        }
    }
}
